﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SmartAPI.FileReader;
using System;
using System.Text.Json;
using System.Threading.Tasks;

namespace FTPUploadFile
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var services = new ServiceCollection();
            var jsonOptions = new JsonSerializerOptions
            {
                Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            };
            services.AddHttpApi<IFileReaderApi>()
               .ConfigureHttpApi(o =>
               {
                   o.HttpHost = new Uri("http://211.155.90.106:9081");
                   o.JsonSerializeOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
                   o.JsonSerializeOptions.IgnoreNullValues = true;
                   o.JsonDeserializeOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
               });
            services.AddLogging(o =>
            {
                o.AddConsole();
            });
            var serviceProvider = services.BuildServiceProvider();

            var fileReadApi = serviceProvider.GetService<IFileReaderApi>();

            var ftpUploadResult = await fileReadApi.UploadFTPFileAsync(new SmartAPI.FileReader.Dto.FtpFileUploadDto
            {
                Input = new[]
                {
                    new SmartAPI.FileReader.Dto.FtpFileInput
                    {
                         FilePath = "pdf/input/990001-041.tif",
                         FtpHost ="127.0.0.1",
                         FtpPassword="password",
                         FtpPort= 21,
                         FtpUsername="filereader",
                         Sequence=1
                    }
                },
                Output = new SmartAPI.FileReader.Dto.FTPFileOutput
                {
                    FilePath = "pdf/output",
                    FtpHost = "127.0.0.1",
                    FtpPassword = "password",
                    FtpPort = 21,
                    FtpUsername = "filereader",
                    Serial = "123"
                }
            });

            Console.WriteLine("FTP上传结果");
            Console.WriteLine(JsonSerializer.Serialize(ftpUploadResult, jsonOptions));

            var ftpTaskResult = await fileReadApi.GetResultAsync(ftpUploadResult.Data);
            Console.WriteLine("查询FTP任务结果");
            Console.WriteLine(JsonSerializer.Serialize(ftpTaskResult, jsonOptions));
        }
    }
}
